def window(page_numbers, size=2, overlap=1):
    """
    按固定窗口 + 滑动步长切分页码列表，使用yield方式返回生成器。
    :param page_numbers: List[int] 页码列表（如 [2,3,4,5,6]）
    :param size: int 窗口大小（每组页数）
    :param overlap: int 重叠数量（滑动步长为size-overlap）
    :yield: 每次生成一个包含窗口内页码的列表
    """
    n = len(page_numbers)
    if n == 0:
        return  # 空列表不生成任何窗口
    
    # 如果列表长度小于窗口大小
    if n < size:
        yield page_numbers.copy()
        return 
        
    # 如果列表长度等于窗口大小，只生成一个窗口
    if n == size:
        yield page_numbers.copy()
        return
    
    # 计算滑动步长
    step = max(1, size - overlap)  # 确保步长至少为1
    
    # 生成完整窗口
    last_possible_start = n - size
    
    for start in range(0, last_possible_start + 1, step):
        yield page_numbers[start:start + size]
    
    # 确保最后一个窗口包含列表的最后一个元素
    last_start = (last_possible_start // step) * step
    if last_start < last_possible_start:
        yield page_numbers[last_possible_start:last_possible_start + size]


def test_window_function():
    print("\n==== 开始测试 sliding_window 函数 ====")
    
    # 测试列表
    test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    # 默认参数测试 (size=2, overlap=0)
    print("\n测试1: 默认参数测试 (size=2, overlap=0):")
    expected1 = [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
    result1 = list(window(test_list, size=2, overlap=0))
    for w in result1:
        print(w)
    print(f"预期结果: {expected1}")
    print(f"实际结果: {result1}")
    print(f"测试通过: {result1 == expected1}")
    
    # 自定义窗口大小测试 (size=3, overlap=1)
    print("\n测试2: 自定义窗口大小测试 (size=3, overlap=1):")
    expected2 = [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
    result2 = list(window(test_list, size=3, overlap=1))
    for w in result2:
        print(w)
    print(f"预期结果: {expected2}")
    print(f"实际结果: {result2}")
    print(f"测试通过: {result2 == expected2}")
    
    # 自定义重叠大小测试 (size=4, overlap=2)
    print("\n测试3: 自定义重叠大小测试 (size=4, overlap=2):")
    expected3 = [[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9]]
    result3 = list(window(test_list, size=4, overlap=2))
    for w in result3:
        print(w)
    print(f"预期结果: {expected3}")
    print(f"实际结果: {result3}")
    print(f"测试通过: {result3 == expected3}")
    
    # 无重叠测试 (size=3, overlap=0)
    print("\n测试4: 无重叠测试 (size=3, overlap=0):")
    expected4 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    result4 = list(window(test_list, size=3, overlap=0))
    for w in result4:
        print(w)
    print(f"预期结果: {expected4}")
    print(f"实际结果: {result4}")
    print(f"测试通过: {result4 == expected4}")
    
    # 边界情况测试: 空列表
    print("\n测试5: 边界情况 - 空列表:")
    empty_list = []
    expected5 = []
    result5 = list(window(empty_list))
    print(f"预期结果: {expected5}")
    print(f"实际结果: {result5}")
    print(f"测试通过: {result5 == expected5}")
    
    # 边界情况测试: 列表长度小于窗口大小
    print("\n测试6: 边界情况 - 列表长度小于窗口大小:")
    short_list = [1, 2]
    expected6 = [[1,2]]
    result6 = list(window(short_list, size=3))
    print(f"预期结果: {expected6}")
    print(f"实际结果: {result6}")
    print(f"测试通过: {result6 == expected6}")
    
    # 极限测试: 窗口大小等于列表长度
    print("\n测试7: 极限情况 - 窗口大小等于列表长度:")
    exact_list = [1, 2, 3]
    expected7 = [[1, 2, 3]]
    result7 = list(window(exact_list, size=3, overlap=0))
    print(f"预期结果: {expected7}")
    print(f"实际结果: {result7}")
    print(f"测试通过: {result7 == expected7}")
    
    print("\n==== sliding_window 函数测试完成 ====")

test_window_function()